<?php

namespace App\Http\Controllers\Manage;

use App\Model\Admin;
use App\Model\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class AdminController extends Controller
{
    //定义一个数组  用来排列查询选项或列表头
    protected static $field = [
        ['id'=>1,'navigation'=>'编号'],
//        ['id'=>2,'navigation'=>'创建时间'],
        ['id'=>2,'navigation'=>'更新时间'],
        ['id'=>3,'navigation'=>'权重'],
        ['id'=>4,'navigation'=>'管理员名称'],
        ['id'=>5,'navigation'=>'角色名称'],
        ['id'=>6,'navigation'=>'上次登录ip'],
        ['id'=>7,'navigation'=>'上次登录时间'],
        ['id'=>8,'navigation'=>'登陆次数'],
        ['id'=>9,'navigation'=>'状态'],
    ];

    /**
     * 管理员列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */

    public function index()
    {
        #获取用户id、角色id
        $id = session('user.id');
        $role_id = session('user.role_id');
        if($role_id == 1){
            $adminInfo = Admin::orderBy('role_id','ASC')->orderBy('rank','DESC')->orderBy('create_time','DESC')->paginate(8);
        }else{
            $adminInfo = Admin::where('id',$id)->get();
        }
        #获取管理员信息 角色越高、权重越大、更新时间越近的优先显示

        foreach ($adminInfo as $k=>$m){
            #获取管理员角色名称
            $adminInfo[$k]->roleName = $this->getRoleName($m->role_id);
            #转变登录ip格式
            $adminInfo[$k]->ip = long2ip($adminInfo[$k]->ip);
        }
        #获取表格头信息
        $navigation = self::$field;
        return view('manage.system.admin.index',['adminInfo'=>$adminInfo,'navigation'=>$navigation,'active1'=>16,'active2'=>1]);
    }

    /**
     * 根据角色id获取角色信息
     * @param $roleId
     * @return mixed
     */
    public function getRoleName($roleId)
    {
        $role = Role::where('id',$roleId)->select('name')->first();
        return $role->name;
    }

    /**
     * 设置管理员状态 启用/禁用
     */
    public function setStatus()
    {
        $id = Input::get('id');
        $status = Input::get('status');
        $res = Admin::setStatus($id,$status);
        if ($res) {
            return [
                'error' => 0,
                'msg' => '设置成功！'
            ];
        } else {
            return [
                'error' => 1,
                'msg' => '设置失败！'
            ];
        }
    }

    /**
     * 删除管理员
     * @return array
     */
    public function delAdmin()
    {
        $id = Input::get('id');
        $res = Admin::delAdmin($id);
        if($res){
            return [
                'error'=>0,
                'msg'=>'删除成功！'
            ];
        }else{
            return [
                'error'=>1,
                'msg'=>'删除失败！'
            ];
        }
    }

    /**
     * 跳转到添加管理员页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add()
    {
        #获取用户角色id
        $role_id = session('user.role_id');
        if($role_id != 1){
            return back()->with('msg','权限不足');
        }
        #获取角色信息
        $role = Role::getInfoAll();
        return view('manage.system.admin.add_admin',['role'=>$role,'active1'=>16,'active2'=>1]);
    }

    /**
     * 添加管理员
     * @return array
     */
    public function addAdmin()
    {
        $validate = Admin::addAdminValidate(Input::all());
        if($validate->fails()){
            $warnings = $validate->messages();
            $show_warning = $warnings->first();
            return [
                'error'=>2,
                'msg'=>$show_warning,
            ];
        }
        $res = Admin::addAdmin(Input::all());
        if ($res) {
            return [
                'error'=>0,
                'msg'=>'添加成功！',
            ];
        }else{
            return [
                'error'=>1,
                'msg'=>'添加失败！',
            ];
        }
    }

    /**
     * 跳转到修改页
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function update($id)
    {
        #获取所有角色信息
        $role = Role::getInfoAll();
        #获取该管理员信息
        $oneInfo = Admin::getOneAdminInfo($id);
        return view('manage.system.admin.update_admin',['role'=>$role,'oneInfo'=>$oneInfo,'active1'=>16,'active2'=>1]);
    }

    /**
     * 提交并操作修改信息
     * @return array
     */
    public function updateAdmin()
    {
        $res = Admin::updateAdmin(Input::all());
        if($res){
            return [
                'error'=>0,
                'msg'=>'ok'
            ];
        }else{
            return [
                'error'=>1,
                'msg'=>'失败！'
            ];
        }
    }
}
